package com.yihu.wlyy.service.app.followup;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;
import org.springside.modules.persistence.SearchFilter.Operator;

import com.yihu.wlyy.entity.follow.FollowUpItemRecord;
import com.yihu.wlyy.entity.follow.FollowUpItemRecordQuestion;
import com.yihu.wlyy.entity.follow.FollowUpItemRecordQuestionOption;
import com.yihu.wlyy.entity.follow.FollowUpPlan;
import com.yihu.wlyy.entity.follow.FollowUpPlanDoctor;
import com.yihu.wlyy.entity.follow.FollowUpPlanStage;
import com.yihu.wlyy.entity.follow.FollowUpPlanStageItem;
import com.yihu.wlyy.entity.follow.FollowUpPlanStageItemQuestion;
import com.yihu.wlyy.entity.follow.FollowUpPlanStageItemQuestionOption;
import com.yihu.wlyy.entity.follow.FollowUpPlanStageTarget;
import com.yihu.wlyy.entity.follow.FollowUpQuestion;
import com.yihu.wlyy.entity.follow.FollowUpTc;
import com.yihu.wlyy.entity.follow.FollowUpTemplate;
import com.yihu.wlyy.entity.follow.FollowUpTemplatePlan;
import com.yihu.wlyy.entity.follow.FollowUpTemplateQuestion;
import com.yihu.wlyy.entity.follow.FollowUpTemplateQuestionOption;
import com.yihu.wlyy.repository.FollowUpItemRecordDao;
import com.yihu.wlyy.repository.FollowUpItemRecordQuestionDao;
import com.yihu.wlyy.repository.FollowUpItemRecordQuestionOptionDao;
import com.yihu.wlyy.repository.FollowUpPlanDao;
import com.yihu.wlyy.repository.FollowUpPlanDoctorDao;
import com.yihu.wlyy.repository.FollowUpPlanStageDao;
import com.yihu.wlyy.repository.FollowUpPlanStageItemDao;
import com.yihu.wlyy.repository.FollowUpPlanStageItemQuestionDao;
import com.yihu.wlyy.repository.FollowUpPlanStageItemQuestionOptionDao;
import com.yihu.wlyy.repository.FollowUpPlanStageTargetDao;
import com.yihu.wlyy.repository.FollowUpQuestionDao;
import com.yihu.wlyy.repository.FollowUpTcDao;
import com.yihu.wlyy.repository.FollowUpTemplateDao;
import com.yihu.wlyy.repository.FollowUpTemplatePlanDao;
import com.yihu.wlyy.repository.FollowUpTemplateQuestionDao;
import com.yihu.wlyy.repository.FollowUpTemplateQuestionOptionDao;
import com.yihu.wlyy.service.BaseService;

/**
 * 随访干预业务处理类型
 * @author George
 *
 */
@Component
@Transactional(rollbackOn = Exception.class)
public class FollowUpService extends BaseService {

	@Autowired
	private FollowUpPlanDao followUpPlanDao;
	@Autowired
	private FollowUpPlanStageDao followUpPlanStageDao;
	@Autowired
	private FollowUpTemplatePlanDao followUpTemplatePlanDao;
	@Autowired
	private FollowUpTemplateDao followUpTemplateDao;
	@Autowired
	private FollowUpTemplateQuestionDao followUpTemplateQuestionDao;
	@Autowired
	private FollowUpTemplateQuestionOptionDao followUpTemplateQuestionOptionDao;
	@Autowired
	private FollowUpPlanStageTargetDao followUpPlanStageTargetDao;
	@Autowired
	private FollowUpTcDao followUpTcDao;
	@Autowired
	private FollowUpQuestionDao followUpQuestionDao;
	@Autowired
	private FollowUpPlanDoctorDao followUpPlanDoctorDao;
	@Autowired
	private FollowUpPlanStageItemDao followUpPlanStageItemDao;
	@Autowired
	private FollowUpPlanStageItemQuestionDao followUpPlanStageItemQuestionDao;
	@Autowired
	private FollowUpPlanStageItemQuestionOptionDao followUpPlanStageItemQuestionOptionDao;
	@Autowired
	private FollowUpItemRecordDao followUpItemRecordDao;
	@Autowired
	private FollowUpItemRecordQuestionDao followUpItemRecordQuestionDao;
	@Autowired
	private FollowUpItemRecordQuestionOptionDao followUpItemRecordQuestionOptionDao;

	/**
	 * 查询与医生相关的随访计划
	 * @param doctor 医生标识
	 * @param type 查询类型：1已发起，2已结束
	 * @param id
	 * @param pagesize 分页大小
	 * @return
	 */
	public Page<FollowUpPlan> findPlanByDoctor(String doctor, int type, long id, int pagesize) {
		if (pagesize <= 0) {
			pagesize = 10;
		}
		// 排序
		Sort sort = new Sort(Direction.DESC, "id");
		// 分页信息
		PageRequest pageRequest = new PageRequest(0, pagesize, sort);
		switch (type) {
		case 1:
			// 已发起
			if (id > 0) {
				return followUpPlanDao.findByDoctor(doctor, id, pageRequest);
			} else {
				return followUpPlanDao.findByDoctor(doctor, pageRequest);
			}
		case 2:
			// 已线束
			if (id > 0) {
				return followUpPlanDao.findByDoctorFinish(doctor, id, pageRequest);
			} else {
				return followUpPlanDao.findByDoctorFinish(doctor, pageRequest);
			}
		}
		return null;
	}

	/**
	 * 查询医生随访计划模板
	 * @param doctor 医生标识
	 * @param id
	 * @param pagesize
	 * @return
	 */
	public Page<FollowUpTemplatePlan> findPlanTemplateByDoctor(String doctor, long id, int pagesize) {
		if (pagesize <= 0) {
			pagesize = 10;
		}
		// 排序
		Sort sort = new Sort(Direction.DESC, "id");
		// 分页信息
		PageRequest pageRequest = new PageRequest(0, pagesize, sort);
		// 设置查询条件
		Map<String, SearchFilter> filters = new HashMap<String, SearchFilter>();
		// 指定医生
		filters.put("doctor", new SearchFilter("doctor", Operator.EQ, doctor));
		if (id > 0) {
			filters.put("id", new SearchFilter("id", Operator.LT, id));
		}
		Specification<FollowUpTemplatePlan> spec = DynamicSpecifications.bySearchFilter(filters.values(), FollowUpTemplatePlan.class);
		return followUpTemplatePlanDao.findAll(spec, pageRequest);
	}

	/**
	 * 查询医生的随访模板
	 * @param doctor 医生标识
	 * @param kind 查询类型：0全部模板，1系统模板，2自定义模板
	 * @param type 模板类型：0全部类型，1问卷模板，2体测模板
	 * @param id
	 * @param pagesize 分页大小
	 * @return
	 */
	public Page<FollowUpTemplate> findTemplateByDoctor(String doctor, int kind, int type, long id, int pagesize) {
		if (pagesize <= 0) {
			pagesize = 10;
		}
		// 排序
		Sort sort = new Sort(Direction.DESC, "id");
		// 分页信息
		PageRequest pageRequest = new PageRequest(0, pagesize, sort);

		// 设置查询条件
		Map<String, SearchFilter> filters = new HashMap<String, SearchFilter>();
		if (kind == 1) {
			// 查询系统模板
			filters.put("doctor", new SearchFilter("doctor", Operator.EQ, "0"));
		} else if (kind == 2) {
			// 查询医生自定义模板
			filters.put("doctor", new SearchFilter("doctor", Operator.EQ, doctor));
		}
		if (type == 1 || type == 2) {
			// 模板类型过滤
			filters.put("doctor", new SearchFilter("type", Operator.EQ, type));
		}
		if (id > 0) {
			filters.put("id", new SearchFilter("id", Operator.LT, id));
		}
		Specification<FollowUpTemplate> spec = DynamicSpecifications.bySearchFilter(filters.values(), FollowUpTemplate.class);
		return followUpTemplateDao.findAll(spec, pageRequest);
	}

	/**
	 * 阶段目标列表查询
	 * @return
	 */
	public List<FollowUpPlanStageTarget> findTargetList() {
		return followUpPlanStageTargetDao.findAll();
	}

	/**
	 * 查询体测题目列表
	 * @param doctor 医生标识
	 * @param type 查询类型：0全部，1系统，2自定义
	 * @param id
	 * @param pagesize 分页大小
	 * @return
	 */
	public Page<FollowUpTc> findTcList(String doctor, int type, long id, int pagesize) {
		if (pagesize <= 0) {
			pagesize = 10;
		}
		// 排序
		Sort sort = new Sort(Direction.DESC, "id");
		// 分页信息
		PageRequest pageRequest = new PageRequest(0, pagesize, sort);

		// 设置查询条件
		Map<String, SearchFilter> filters = new HashMap<String, SearchFilter>();
		if (type == 1) {
			// 查询系统问题
			filters.put("doctor", new SearchFilter("doctor", Operator.EQ, "0"));
		} else if (type == 2) {
			// 查询医生自定义问题
			filters.put("doctor", new SearchFilter("doctor", Operator.EQ, doctor));
		}
		if (id > 0) {
			filters.put("id", new SearchFilter("id", Operator.LT, id));
		}
		Specification<FollowUpTc> spec = DynamicSpecifications.bySearchFilter(filters.values(), FollowUpTc.class);
		return followUpTcDao.findAll(spec, pageRequest);
	}

	/**
	 * 查询问卷题目列表
	 * @param doctor 医生标识
	 * @param type 查询类型：0全部，1系统，2自定义
	 * @param id
	 * @param pagesize 分页大小
	 * @return
	 */
	public Page<FollowUpQuestion> findQuestionList(String doctor, int type, long id, int pagesize) {
		if (pagesize <= 0) {
			pagesize = 10;
		}
		// 排序
		Sort sort = new Sort(Direction.DESC, "id");
		// 分页信息
		PageRequest pageRequest = new PageRequest(0, pagesize, sort);

		// 设置查询条件
		Map<String, SearchFilter> filters = new HashMap<String, SearchFilter>();
		if (type == 1) {
			// 查询系统问题
			filters.put("doctor", new SearchFilter("doctor", Operator.EQ, "0"));
		} else if (type == 2) {
			// 查询医生自定义问题
			filters.put("doctor", new SearchFilter("doctor", Operator.EQ, doctor));
		}
		if (id > 0) {
			filters.put("id", new SearchFilter("id", Operator.LT, id));
		}
		Specification<FollowUpQuestion> spec = DynamicSpecifications.bySearchFilter(filters.values(), FollowUpQuestion.class);
		return followUpQuestionDao.findAll(spec, pageRequest);
	}

	/**
	 * 随访模板保存
	 * @param template 模板对象
	 * @param questionList 模板问题列表
	 * @param optionsList 问题选项列表
	 */
	public void saveTemplate(FollowUpTemplate template, List<FollowUpTemplateQuestion> questionList, List<FollowUpTemplateQuestionOption> optionsList) {
		followUpTemplateDao.save(template);
		followUpTemplateQuestionDao.save(questionList);
		followUpTemplateQuestionOptionDao.save(optionsList);
	}

	/**
	 * 查询随访计划模板信息
	 * @param template
	 * @return
	 */
	public FollowUpTemplatePlan findPlanTemplateByCode(String template) {
		return followUpTemplatePlanDao.findByCode(template);
	}

	/**
	 * 添加随访计划模板
	 * @param FollowUpTemplatePlan 随访计划模板对象
	 */
	public void addPlanTemplate(FollowUpTemplatePlan planTemplate) {
		followUpTemplatePlanDao.save(planTemplate);
	}

	/**
	 * 删除随访计划模板
	 * @param planTemplate
	 */
	public void deletePlanTemplate(FollowUpTemplatePlan planTemplate) {
		followUpTemplatePlanDao.delete(planTemplate);
	}

	/**
	 * 添加随访计划
	 * @param planList 随访计划列表
	 * @param planDoctorList 医生相关的随访计划列表
	 * @param stageList 随访阶段列表
	 * @param stageItemList 随访项列表
	 * @param questionList 随访问题列表
	 * @param optionList 随访问题选项列表
	 * @param recordList 随访记录列表
	 * @param recordQuestionList 随访记录问题列表
	 * @param recordQuestionOptionList 随访记录问题选项列表
	 */
	public void addPlan(List<FollowUpPlan> planList, List<FollowUpPlanDoctor> planDoctorList, List<FollowUpPlanStage> stageList, List<FollowUpPlanStageItem> stageItemList, List<FollowUpPlanStageItemQuestion> questionList, List<FollowUpPlanStageItemQuestionOption> optionList, List<FollowUpItemRecord> recordList, List<FollowUpItemRecordQuestion> recordQuestionList, List<FollowUpItemRecordQuestionOption> recordQuestionOptionList) {
		followUpPlanDao.save(planList);
		followUpPlanDoctorDao.save(planDoctorList);
		followUpPlanStageDao.save(stageList);
		followUpPlanStageItemDao.save(stageItemList);
		followUpPlanStageItemQuestionDao.save(questionList);
		followUpPlanStageItemQuestionOptionDao.save(optionList);
		followUpItemRecordDao.save(recordList);
		followUpItemRecordQuestionDao.save(recordQuestionList);
		followUpItemRecordQuestionOptionDao.save(recordQuestionOptionList);
	}

	/**
	 * 查询随访模板详情
	 * @param template
	 * @return
	 */
	public FollowUpTemplate findTemplateInfo(String template) {
		return followUpTemplateDao.findByCode(template);
	}

	/**
	 * 查询模板问题列表
	 * @param template
	 * @return
	 */
	public List<FollowUpTemplateQuestion> findTemplateQuestions(String template) {
		return followUpTemplateQuestionDao.findByTemplate(template);
	}

	/**
	 * 查询模板对象
	 * @param template
	 * @return
	 */
	public FollowUpTemplate findTemplateByCode(String template) {
		return followUpTemplateDao.findByCode(template);
	}
	
	/**
	 * 删除随访模板
	 * @param template
	 */
	public void deleteTemplate(String template){
		followUpTemplateDao.deleteByCode(template);
		followUpTemplateQuestionDao.deleteByTemplate(template);
		followUpTemplateQuestionOptionDao.deleteByTemplate(template);
	}

}
